package com.zr.task.presenter;


import com.zh.baselibrary.base.BasePresenter;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.utils.ListUtils;
import com.zh.baselibrary.utils.LogUtils;
import com.zh.baselibrary.views.ToastView;
import com.zr.andealib.util.HardEvent;
import com.zr.andealib.util.LockUtil;
import com.zr.andealib.util.constant.AndeaConstant;
import com.zr.task.presenter.view.EmergencyView;
import com.zr.task.utils.EquipmentTypeUtils;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.util.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Semaphore;

public class EmergencyPresenter extends BasePresenter<EmergencyView> {
    private LockUtil lockUtil;
    private List<Integer> OpenedDoor = new ArrayList<>();
    private Timer timer;
    private Semaphore semaphore = new Semaphore(1, true);


    public EmergencyPresenter() {
        lockUtil = new LockUtil();
        lockUtil.init();
    }

    public void toOpenUi(int uiBox) {
        int boxNum = uiToHard(uiBox);
        lockUtil.openDoor(boxNum);
    }

    private int[] uiToDevice8N = {1, 0, 6, 2, 4, 7, 3, 5, 8};
    private int[] uiToDevice11N = {1, 0, 6, 9, 2, 4, 7, 10, 3, 5, 8, 11};

    private int uiToHard(int boxNum) {
        int totalNum = EquipmentTypeUtils.getBoxNum(getContext());
        if (boxNum <= totalNum) {
            if (totalNum <= 2)
                return boxNum;
            else if (totalNum == 8)
                return uiToDevice8N[boxNum];
            else
                return uiToDevice11N[boxNum];
        } else {
            return boxNum;
        }
    }

    private int[] deviceToUI8N = {0, 0, 3, 6, 4, 7, 2, 5, 8};
    private int[] deviceToUI11N = {0, 0, 4, 8, 5, 9, 2, 6, 10, 3, 7, 11};

    private int hardNumToUi(int boxNum) {
        int totalNum = EquipmentTypeUtils.getBoxNum(getContext());
        if (boxNum <= totalNum) {
            if (totalNum <= 2)
                return boxNum;
            else if (totalNum == 8)
                return deviceToUI8N[boxNum];
            else
                return deviceToUI11N[boxNum];
        } else {
            return boxNum;
        }
    }

    public boolean canOut() {
        boolean isOpen = false;
            for (int i = 1; i <= EquipmentTypeUtils.getBoxNum(getContext()); i++) {
               boolean one = lockUtil.isDoorOPen(i);
                if (one) {
                    if (mViewRef != null && getView() != null)
                        getView().notClosedBefore(hardNumToUi(i),false);
                        isOpen = true;
                    checkDoorClose(i);
                }
            }

        return  !isOpen;
    }


    public void checkDoorClose(int num) {
        try {
            semaphore.acquire();
            if (!OpenedDoor.contains(num))
                OpenedDoor.add(num);
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        if (OpenedDoor.size() == 1) {
            if (timer == null)
                timer = new Timer();

            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    for (int i = 0; i < OpenedDoor.size(); i++) {
                        try {
                            semaphore.acquire();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        int boxNum = OpenedDoor.get(i);
                        semaphore.release();
                        boolean isOpen = lockUtil.isDoorOPen(boxNum);
                        if (!isOpen) {
                            OpenedDoor.remove(i);
                            i--;
                            if (mViewRef != null && getView() != null)
                                getView().doorClosed(boxNum, hardNumToUi(boxNum));
                            else  //开启盘点
                                EventBus.getDefault().post(new HardEvent(boxNum, AndeaConstant.CALL_EMERGENCY));

                        }
                    }
                    if (OpenedDoor.size() == 0) {
                        timer.cancel();
                        timer.purge();
                        timer = null;
                    }
                }
            }, 0, 1000);
        }
    }


}
